<!DOCTYPE HTML>
<script src="../resources/gc.js"></script>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<!--

Accessibility Object Model
Explainer: https://github.com/WICG/aom/blob/master/explainer.md
Spec: https://wicg.github.io/aom/spec/

-->

<div id="button1" role="button">Click</div>

<script>

test(function(t) {
    assert_true(internals.runtimeFlags.accessibilityObjectModelEnabled);
}, "Make sure that Accessibility Object Model is enabled");

promise_test(async t => {
    const element = document.getElementById("button1");
    let computedAXNode = await getComputedAccessibleNode(element);
    assert_true(computedAXNode != null);
}, 'Ensure that a non null value is returned from getComputedAccessibleNode');

promise_test(async t => {
    const element = document.getElementById("button1");
    let computedAXNode1 = await getComputedAccessibleNode(element);
    let computedAXNode2 = await getComputedAccessibleNode(element);
    assert_true(computedAXNode1 === computedAXNode2);
}, "Multiple calls to getComputedAccessibleNode will return the same node.");
</script>

<div id="button2" role="button" aria-label="axButton">Click</div>

<script>

promise_test(async t => {
    var button1 = document.getElementById("button1");
    let button1CAXNode = await getComputedAccessibleNode(button1);

    assert_equals(button1CAXNode.name, "Click");
    assert_equals(button1CAXNode.role, "button");

    button1.style.display = "none";

    var button2 = document.getElementById("button2");
    let button2CAXNode = await getComputedAccessibleNode(button2);
    assert_equals(button2CAXNode.name, "axButton");
    assert_equals(button2CAXNode.role, "button");

    // As button1 has no node in the accessibility tree anymore, assert that the
    // its previously retrieved computed accessible node has had its attributes
    // nullified.
    assert_equals(button1CAXNode.name, null);
    assert_equals(button1CAXNode.role, null);

}, "Deleting nodes from the accessibility tree will not cause a crash, and properties on any references to a deleted computed accessible node have been nullified.");

</script>

<div id="radio1" role="radio">Radio 1</div>
<div id="radio2" role="radio">Radio 2</div>

<script>

promise_test(async function(test) {
    var radio1 = document.getElementById("radio1");
    var caxNode1 = await window.getComputedAccessibleNode(radio1);
    var radio2 = document.getElementById("radio2");
    var caxNode2 = await window.getComputedAccessibleNode(radio2);
    radio1.innerHTML = "pizza";
    radio2.innerHTML = "tacos";

    // ComputedAccessibleNode for this element has not been updated so should
    // only reflect the name at the time of its request.
    assert_equals(caxNode1.name, "Radio 1");
    assert_equals(caxNode2.name, "Radio 2");

    // Manually refresh the node and check that all references to
    // ComputedAccessibleNodes have been updated.
    await caxNode1.ensureUpToDate();
    assert_equals(caxNode1.name, "pizza");
    assert_equals(caxNode2.name, "tacos");

    radio1.setAttribute("aria-label", "pasta");
    assert_equals(caxNode1.name, "pizza");

    await caxNode1.ensureUpToDate();
    assert_equals(caxNode1.name, "pasta");
}, "ComputedAccessibleNode.ensureUpToDate");



</script>
